#ifndef _CONNECT_RPC_SESSION_H_
#define _CONNECT_RPC_SESSION_H_

#include "IoSocketEx.h"
#include "Header.h"
#include "Lock.h"
#include <string>
#include <vector>
#include <deque>

class CConnSession
{
public:
    enum
    {
        E_ConnSession_Break,
        E_ConnSession_Connecting,
        E_ConnSession_Connected,
    };

    CConnSession(u64 sid=0, const char *ip="", s32 port=0, SGLib::IPacketParser *parser=NULL);
    ~CConnSession();
    u64 GetId(){ return m_sid; }
    const char* GetIp(){ return m_ip.c_str(); }
    s32 GetPort(){ return m_port; }
    void OnConnected(SGLib::CIoSocketEx *session);
    void OnClose();
    void SendMsg(u32 msgid, const IMessage &msg);
    void SendMsg(const byte *data, s32 len);
	void SendMsgWithHead(u32 msgid, const IMessage &msg, char *head, int len);
    void Connect();

private:
    void _Connect();
    void _DoSendMsgCache();
    void _SetState(s8 state);
    s8 _GetState();

protected:
    u64 m_sid;
    std::string m_ip;
    s32 m_port;
    SGLib::CIoSocketEx *m_baseSession;
    SGLib::IPacketParser *m_parser;
    s8 m_state;
    SGLib::CLock m_stateLock;

    std::deque<std::string> m_msgs;
};

#endif

